﻿# coding=utf-8
from collections import deque
import numpy as np
import time
# import imutils
import cv2
import time

# 设定红色阈值，HSV空间

redLower = np.array([170, 100, 100])
redUpper = np.array([179, 255, 255])

#redLower = np.array([88, 44, 160])
#redUpper = np.array([103, 230, 255])

# 初始化追踪点的列表

mybuffer = 16
pts = deque(maxlen=mybuffer)
counter = 0

# 打开摄像头
camera = cv2.VideoCapture(0)

# 等待两秒
time.sleep(3)

# 遍历每一帧，检测红色瓶盖

while True:

    # 读取帧
    (ret, frame) = camera.read()
    # frame = cv2.resize(frame,(800, 600))
    # 判断是否成功打开摄像头
    if not ret:
        print
        'No Camera'
        break

    # frame = imutils.resize(frame, width=600)
    # 转到HSV空间
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    print '\r\n'
    print '1', time.clock()
    # 根据阈值构建掩膜
    mask = cv2.inRange(hsv, redLower, redUpper)

    print '2', time.clock()
    # 腐蚀操作
    mask = cv2.erode(mask, None, iterations=2)

    # 膨胀操作，其实先腐蚀再膨胀的效果是开运算，去除噪点
    mask = cv2.dilate(mask, None, iterations=2)

    print '3', time.clock()
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]

    # 初始化瓶盖圆形轮廓质心
    center = None

    # 如果存在轮廓
    if len(cnts) > 0:
        # 找到面积最大的轮廓
        c = max(cnts, key=cv2.contourArea)

        # 确定面积最大的轮廓的外接圆
        ((x, y), radius) = cv2.minEnclosingCircle(c)

        # 计算轮廓的矩
        M = cv2.moments(c)

        # 计算质心
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

        # 只有当半径大于10时，才执行画图
        if radius > 10:
            cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
            print("x = ",x,"y=",y)
            cv2.circle(frame, center, 5, (0, 0, 255), -1)

    cv2.imshow('Frame', frame)
    # 键盘检测，检测到esc键退出
    k = cv2.waitKey(1) & 0xFF
    counter += 1
    if k == 27:
        break

# 摄像头释放
camera.release()

# 销毁所有窗口
cv2.destroyAllWindows()
